package com.centronix.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centronix.bean.ComboBean;
import com.centronix.dao.ProductDAO;
import com.centronix.domain.Product;
import com.centronix.manager.ProductManager;

@Service("productManager")
@Transactional
public class ProductManagerImpl implements ProductManager{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ProductDAO productDAO;
	
	public List<Product> findAllProduct(){
		return productDAO.findAll();
	}
	
	public Boolean saveProduct(Product product){
		productDAO.saveAndFlush(product);
		return true;
	}
	
	public List<ComboBean> getProductCombo(int productType){
		List<ComboBean> comboBeans = new ArrayList<>();
		comboBeans.add(ComboBean.getDefaultCombo());
		List<Product> products= productDAO.findProductByProductType(productType);
		ComboBean comboBean = null;
		for (Product product : products) {
			comboBean = new ComboBean();
			copyProductToComboBean(product, comboBean);
			comboBeans.add(comboBean);
		}
		return comboBeans;
	}
	
	public void copyProductToComboBean(final Product product, final ComboBean comboBean) {
		comboBean.setValue(String.valueOf(product.getProductId()));
		comboBean.setLabel(product.getNarrative());
	}
	
	public Boolean deleteProduct(int productId){
		productDAO.delete(productId);
		return true;
	}

	public ProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}
	
}
